package com.briup.day18;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ThinkBook
 */
public class Test04_List {
    //LinkedList中头尾节点操作方法
    public static void main(String[] args) {
        //1.实例化集合对象
        //注意，要测试LinkedList中的方法，必须用LinkedList引用指向LinkedList对象
        LinkedList<String> list = new LinkedList<>();

        String s1 = "hello";
        String s2 = "world";
        String s3 = "nihao";

        //2.往集合中添加元素并输出
        list.add(s1);
        list.add(s2);
        list.add(s3);
        System.out.println("list: " + list);

        System.out.println("------------");

        //3.往头、尾节点添加元素
        list.addFirst("first");
        list.addLast("last");
        System.out.println("list: " + list);

        System.out.println("------------");

        //4.获取头尾节点元素
        System.out.println("getFirst: " + list.getFirst());
        System.out.println("getLast: " + list.getLast());

        System.out.println("------------");

        //5.删除头尾节点元素
        list.removeFirst();
        list.removeLast();
        System.out.println("list: " + list);
    }
    //ArrayList和LinkedList的性能测试
    public static void main02(String[] args) {
        //操作集合的次数
        final int NUM = 100000;

        //1.实例化集合对象
        //List<String> list = new ArrayList<>();
        List<String> list = new LinkedList<>();

        //2.开启计时，往集合种放入 100000 个元素
        long start1 = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.add(0,"hello"+i);
        }
        long end1 = System.currentTimeMillis();

        //3.输出时长
        System.out.println(list.getClass().getSimpleName()+"插入"+NUM+"条数据耗时"+(end1-start1)+"毫秒");

        //4.开启计时，从集合种取 100000 个元素
        long start2 = System.currentTimeMillis();
        for(int i = 0; i < list.size(); i++){
            list.get(i);
        }
        long end2 = System.currentTimeMillis();

        //5.输出时长
        System.out.println(list.getClass().getSimpleName()+"检索"+NUM+"条数据耗时"+(end2-start2)+"毫秒");

    }
    public static void main01(String[] args) {
        //实例化集合对象
        List<String> list = new ArrayList<>();
        //添加数据
        //添加元素，默认尾部添加
        list.add("hello");
        list.add(0,"123");//123 hello
        //修改指定位置元素
        list.set(1,"world");//123 world
        list.add(0,"abcd");//abcd 123 world
        list.add(1,"test");//abcd test 123 world
        list.add(2,"briup");//abcd test briup 123 world
        System.out.println(list);

        //indexOf
        int index = list.indexOf("briup");
        System.out.println("briup index: " + index);
        index = list.lastIndexOf("briup");
        System.out.println("briup last index: " + index);

        System.out.println("--------------");

        //实际取值：[fromIndex,toIndex)
        List<String> sList = list.subList(1, 3);      // test briup
        System.out.println("subList(1,3): " + sList);

        //addAll(index,coll)
        list.addAll(4,sList);   // abcd test briup 123 test briup world
        System.out.println("after addAll, list: " + list);

        System.out.println(list);


        String s = list.get(1);
        System.out.println("index=1 : " + s);  // 123

        list.remove(2); //remove "world"
        System.out.println("after remove 2: " + list);

        System.out.println("——————借助get遍历------- ");
        for(int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + "  ");
        }
        System.out.println();

        System.out.println("-----------------");

        //Collection<String> coll = list;
        //coll.get(2);
    }

}
